<?php
class Administration extends CI_Controller {
	private $narticle;
	private $nuser;
	private $nstatut;
	private $nparametre;
	
	public function index($page = 'administration') {
		log_message('info', '### Controleur administration avec fonction \''.$page.'\'');
		$narticle = "NULL";
		$nuser    = "NULL";
		$nstatut  = "NULL";
		$nparametre = 'NULL';
		
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$this->load->helper('form');
		
		$aData = array(
			'set_columns' => array(
				array(
					'label' => 'Code',
					'name' => 'code',
					'width' => 60,
					'size' => 10,
					'align' => 'right'				
				),
				array(
					'label' => 'Validit&eacute;',
					'name' => 'validite',
					'width' => 80,
					'size' => 10,
					'align' => 'right'				
				),
				array(
					'label' => 'Nom article',
					'name' => 'libelle',
					'width' => 450,
					'size' => 10
				),
				array(
					'label' => 'Poids (g)',
					'name' => 'poids',
					'width' => 70,
					'size' => 10,
					'align' => 'right'
				),
				array(
					'label' => 'Prix unitaire',
					'name' => 'pu',
					'width' => 100,
					'align' => 'right',
					'size' => 10,
					'formatter' => 'currency'
				),
				array(
					'label' => 'id',
					'name' => 'categorieId',
					'width' => 50,
					'hidden' => true,
					'size' => 5
				),
				array(
					'label' => 'Cat&eacute;gorie',
					'name' => 'categorie',
					'width' => 170,
					'size' => 10
				),
				array(
					'label' => 'S&eacute;l',
					'name' => 'sel',
					'width' => 30,
					'size' => 1
				)						
			),
			'div_name' => 'articles_grid',
			'source' => 'administration/articlesListe/',
			'sort_name' => 'libelle',
			'caption' => 'Articles',
			'primary_key' => 'libelle',
			'functionDblClick' => 'AdblClick',
			'functionSelectRow' => 'AselectRow',
			'pager' => 'apager',
			'grid_height' => 450
		);
		
		$uData = array(
			'set_columns' => array(
				array(
					'label' => 'Nom',
					'name' => 'nom',
					'width' => 170,
					'size' => 10					
				),
				array(
					'label' => 'Pr&eacutenom',
					'name' => 'prenom',
					'width' => 170,
					'size' => 10
				),
				array(
					'label' => 'Login',
					'name' => 'login',
					'width' => 130,
					'size' => 10
				),
				array(
					'label' => 'E-mail',
					'name' => 'email',
					'width' => 250,
					'size' => 10
				),				
				array(
					'label' => 'Admin',
					'name' => 'admin',
					'width' => 50,
					'align' => 'right',
					'size' => 10
				)
			),
			'div_name' => 'users_grid',
			'source' => 'administration/usersListe/',
			'sort_name' => 'nom',
			'caption' => 'Utilisateurs enregistr&eacute;s',
			'primary_key' => 'nom',
			'functionDblClick' => 'UdblClick',
			'functionSelectRow' => 'UselectRow',
			'pager' => 'upager',
			'grid_height' => 450
		);
		
		$sData = array(
			'set_columns' => array(
				array(
					'label' => 'identifiant',
					'name' => 'id',
					'width' => 90,
					'size' => 10					
				),
				array(
					'label' => 'libell&eacute;',
					'name' => 'libelle',
					'width' => 550,
					'size' => 10					
				),
				array(
					'label' => 'Suivant',
					'name' => 'suivantId',
					'width' => 90,
					'size' => 10					
				),
			),
			'div_name' => 'statuts_grid',
			'source' => 'administration/statutsListe/',
			'sort_name' => 'id',
			'caption' => 'Enchainement des statuts',
			'primary_key' => 'id',
			'functionDblClick' => 'SdblClick',
			'functionSelectRow' => 'SselectRow',
			'pager' => 'spager',
			'grid_height' => 450
		);
		
		$pData = array(
			'set_columns' => array(
				array(
					'label' => 'param&egrave;tre',
					'name' => 'parametre',
					'width' => 200,
					'size' => 10					
				),
				array(
					'label' => 'Valeur',
					'name' => 'valeur',
					'width' => 600,
					'size' => 350					
				),
			),
			'div_name' => 'params_grid',
			'source' => 'administration/paramsListe/',
			'sort_name' => 'id',
			'caption' => 'Param&egrave;tres',
			'primary_key' => 'id',
			'functionDblClick' => 'PdblClick',
			'functionSelectRow' => 'PselectRow',
			'pager' => 'ppager',
			'grid_height' => 450
		);
		
		$cData = array(
			'set_columns' => array(
				array(
					'label' => 'Cat&eacute;gorie',
					'name' => 'libelle',
					'width' => 750,
					'size' => 300
				),
			),
			'div_name' => 'categories_grid',
			'source' => 'administration/CategoriesListe/',
			'sort_name' => 'id',
			'caption' => 'Cat&eacute;cories',
			'primary_key' => 'id',
			'functionDblClick' => 'CdblClick',
			'functionSelectRow' => 'CselectRow',
			'pager' => 'cpager',
			'grid_height' => 450
		);
		
		
		$aBtn = $this->input->post('BArticles');
		$uBtn = $this->input->post('BUsers');
		$sBtn = $this->input->post('BStatuts');
		$pBtn = $this->input->post('BParams');
		$narticle   = $this->input->post('NArticle');
		$nuser      = $this->input->post('NUser');
		$nstatut    = $this->input->post('NStatut');
		$nparametre = $this->input->post('NParam');
		if($narticle == "")  { $narticle = "NULL"; }
		if($nuser == "")  { $nuser = "NULL"; }
		if($nparametre == "") { $nparametre = "NULL"; }
		
		if(($aBtn == "Retour") || ($uBtn == "Retour") || ($sBtn == "Retour") || ($pBtn == "Retour")) { redirect("commandes"); }
		else {
			if($aBtn == "Gestion du prix") {
				if($narticle != "NULL") { redirect('administration/articlePrix/'.$narticle); }
				else {
					$article_a_jour = array(
						'message' => 'Il faut s&eacute;lectionner un article !!',
						'mes_aff' => 'O');
					$this->session->set_userdata($article_a_jour);
					redirect('administration');
				}
			} else {
				$data['articlesGrille'] = buildGrid($aData);
				$data['usersGrille']    = buildGrid($uData);
				$data['statutsGrille']  = buildGrid($sData);
				$data['paramsGrille']   = buildGrid($pData);
				$data['categoriesGrille'] = buildGrid($cData);
				$data['connecte'] = $this->session->userdata['prenom'].' '.$this->session->userdata['nom'];
				$data['iduser'] = $this->session->userdata['id'];
				$data['admin'] = $this->session->userdata['admin'];
				$data['fonction'] = $page;
				$data['uri'] = $this->uri->segment(1);
				
				//Remplissage de la liste des catégories
				$this->load->model('categories_model', 'DBCategorie', TRUE);
				$result = $this->DBCategorie->read(); // select * from categorie
				$categ = $result->result_array();
				$data['categorie'] = $categ;
		
				$this->load->library('layout');
				$this->layout->set_titre('RAVIOLES');
				$this->layout->ajouter_css('ravioles');
				$this->layout->ajouter_css('jquery-ui');
				$this->layout->ajouter_css('ui.jqgrid');
				$this->layout->ajouter_js('jquery');
				$this->layout->ajouter_js('jquery.corner');
				$this->layout->ajouter_js('jquery-ui');
				$this->layout->ajouter_js('grid.locale-fr');
				$this->layout->ajouter_js('jquery.jqgrid');
				$this->layout->views('templates/header', $data)
			    		 	 ->views('administration/administration',$data)
			    		 	 ->view('templates/footer', $data);
			}
		}
	}
	
	public function articlesListe() {
		buildGridData(array(
				'model' => 'articles_model',
				'method' => 'articlesJqgrid',
				'pkid' => 'id',
				'columns' => array( 'code', 'validite','libelle','poids','pu', 'categorieId', 'categorie' )
				)
		);
	}
	
	public function articleAjout() {
		
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$article = array();
		$article['code']        = $this->input->post('code');
		$article['validite']    = FR2MySQL($this->input->post('validite'));
		$article['libelle']     = $this->input->post('libelle');
		$article['poids']       = $this->input->post('poids');
		$article['pu']          = $this->input->post('pu');
		$article['categorieId']   = $this->input->post('categorie');
		
		$article2 = array(); //champs non échappés : il n'y en a pas.
	
	
		$this->load->model('articles_model','DBArticle', TRUE);
		$resultat = $this->DBArticle->create($article, $article2);
		if($resultat) {
			$article_a_jour = array(
				'message' => 'Article ajout&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($article_a_jour);					
		}
	
		redirect("/administration");
	}
	
	public function articleMiseajour() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		log_message("info", "### MAJ de l'article ".$this->input->post('id')." ###");
		
		$article = array();
		$article['code']        = $this->input->post('code2');
		$article['validite']    = FR2MySQL($this->input->post('validite2'));
		$article['libelle']     = $this->input->post('libelle2');
		$article['poids']       = $this->input->post('poids2');
		$article['pu']          = $this->input->post('pu2');
		$article['categorieId']   = $this->input->post('categorie2');
		log_message('info', '### Catégorie retournée : '.$article['categorieId']." ###");
		
		$article2 = array(); //champs non échappés : il n'y en a pas.
		
		$where = array('id' => $this->input->post('id'));
		
		$this->load->model('articles_model','DBArticle', TRUE);
		$resultat = $this->DBArticle->update($where, $article, $article2);
		if($resultat) {
			$article_a_jour = array(
				'message' => 'Article '.$this->input->post('id').' modifi&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($article_a_jour);			
			}
		
		redirect("/administration");
		
	}
	
	public function articleSuppression() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		$narticle = $this->input->post('ids');	
		$this->session->set_userdata(array('Narticle' => $narticle));
		error_log("RAVIOLES[INFO] - Suppression de l'article ".$narticle,0);
		$this->load->model('articles_model','DBArticle', TRUE);
		$where = array('id' => $narticle);
		$resultat = $this->DBArticle->delete($where);
		if($resultat) {
			$article_a_jour = array(
				'message' => 'Article '.$narticle.' supprim&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($article_a_jour);			
		}
		redirect('/administration');
	}
	
	public function articlePrix($numero = 'NULL') {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$this->load->helper('form');
		
		$aData = array(
			'set_columns' => array(
				array(
					'label' => 'Date',
					'name' => 'datefin',
					'width' => 100,
					'size' => 10
				),
				array(
					'label' => 'Prix',
					'name' => 'prix',
					'width' => 100,
					'align' => 'right',
					'size' => 10,
					'formatter' => 'currency'
				)	
			),
			'div_name' => 'prixarticle_grid',
			'source' => 'administration/prixListe/',
			'sort_name' => 'date',
			'caption' => 'Historique de prix',
			'primary_key' => 'date',
			'pager' => 'pager',
			'functionDblClick' => 'dblClick',
			'functionSelectRow' => 'selectRow',
			'grid_height' => 200
		);

		$pBtn = $this->input->post('BPrix');
		if($pBtn == "Retour") { redirect("administration"); }
		else {
			$this->load->model('articles_model','DBArticle', TRUE);
			$resultat = $this->DBArticle->read('libelle', array('id' => $numero));
			$libarticle = $resultat->result_array();
		
			$data['prixarticleGrille'] = buildGrid($aData);
			$data['connecte'] = $this->session->userdata['prenom'].' '.$this->session->userdata['nom'];
			$data['iduser'] = $this->session->userdata['id'];
			$data['admin'] = $this->session->userdata['admin'];
			$data['uri'] = $this->uri->segment(1);
			$data['libarticle'] = $libarticle;
			
			$this->session->set_userdata('NArticle', $numero);
			
			$this->load->library('layout');
			$this->layout->set_titre('RAVIOLES');
			$this->layout->ajouter_css('ravioles');
			$this->layout->ajouter_css('jquery-ui');
			$this->layout->ajouter_css('ui.jqgrid');
			$this->layout->ajouter_js('jquery');
			$this->layout->ajouter_js('jquery.corner');
			$this->layout->ajouter_js('jquery-ui');
			$this->layout->ajouter_js('grid.locale-fr');
			$this->layout->ajouter_js('jquery.jqgrid');
			$this->layout->views('templates/header', $data)
		    			 ->views('administration/prixArticle',$data)
		    			 ->view('templates/footer', $data);
		}

	}
	
	public function prixListe() {
		buildGridData(array(
				'model' => 'prix_model',
				'method' => 'prixJqgrid',
				'pkid' => 'id',
				'columns' => array( 'datefin', 'prix' )
				)
		);
	}
	
	public function usersListe() {
		buildGridData(array(
				'model' => 'login_model',
				'method' => 'usersJqgrid',
				'pkid' => 'id',
				'columns' => array( 'nom', 'prenom', 'login','email','admin' )
				)
		);
	}
	
	public function user() {
		$this->index('user');
	}
	
	public function userMiseajour() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		log_message("info", "### MAJ de l'utilisateur ".$this->input->post('iduser')." ###");
		
		$user = array();
		$user['nom']    = $this->input->post('nom');
		$user['prenom'] = $this->input->post('prenom');
		$user['login']  = $this->input->post('login');
		$user['email']  = $this->input->post('email');
		$user['admin']  = $this->input->post('admin');
		
		$user2 = array(); //champs non échappés : il n'y en a pas.
		
		$where = array('id' => $this->input->post('iduser'));
		
		$this->load->model('login_model','DBUser', TRUE);
		$resultat = $this->DBUser->update($where, $user, $user2);
		if($resultat) {
			$user_a_jour = array(
				'message' => 'Utilisateur :<br>'.$this->input->post('iduser').' '.$this->input->post('prenom').' '.$this->input->post('nom').'<br>modifi&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($user_a_jour);		
		}
		
		redirect('/administration/user');		
	}
	
	public function userSuppression() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		$nuser = $this->input->post('ids');	
		$this->session->set_userdata(array('NUser' => $nuser));
		log_message('info','#### Suppression du user '.$nuser.' ###');
		$this->load->model('login_model','DBUser', TRUE);
		$where = array('id' => $nuser);
		$resultat = $this->DBUser->delete($where);
		if($resultat) {
			$user_a_jour = array(
				'message' => 'Utilisateur '.$nuser.' supprim&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($user_a_jour);		
		}
		redirect('/administration/user');
	}
	
	function userMdp() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		$mdp = $this->input->post('mdp');
		$nuser = $this->input->post('iduser');
		
		log_message('debug', '### Mise à jour du mot de passe du user '.$this->session->userdata['id'].' : '.$mdp);

		$where = array(
			'id' => $this->session->userdata['id']
		);
		$user = array();
		$user2 = array(
			'mdp' => 'AES_ENCRYPT("'.$mdp.'","password")'
		);
		
		$this->load->model('login_model','DBUser', TRUE);
		$resultat = $this->DBUser->update($where, $user, $user2);
		if($resultat) {
			$user_a_jour = array(
				'message' => 'Mot de passe de l\'utilisateur '.$nuser.' modifi&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($user_a_jour);		
		}
		redirect('/administration/monCompte');				
	}
	
	
	public function monCompte() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		if($this->input->post('BMajCompte') == "Retour") { redirect("/".$this->session->userdata('url_en_cours')); }

		$this->form_validation->set_rules('nom', 'Nom', 'required');
		$this->form_validation->set_rules('prenom', 'Pr&eacute;nom', 'required');
		$this->form_validation->set_rules('email', 'E-mail', 'required|valid_email|callback_email_doublon');		
		$this->form_validation->set_rules('login', 'Login', 'required|callback_login_doublon');

		if($this->form_validation->run()) {
			$this->load->model('login_model','DBLogin',TRUE);
			
			$champs_e = array();
			$champs_e['nom']    = $this->input->post('nom');
			$champs_e['prenom'] = $this->input->post('prenom');
			$champs_e['email']  = $this->input->post('email');
			$champs_e['login']  = $this->input->post('login');
			
			$champs_ne = array();
			
			$where = array('id' => $this->session->userdata['id']);
			
			$resultat = $this->DBLogin->update($where, $champs_e, $champs_ne);
			if($resultat) {
				$user_a_jour = array(
					'nom'     => $this->input->post('nom'),
					'prenom'  => $this->input->post('prenom'),
					'email'   => $this->input->post('email'),
					'login'   => $this->input->post('login'),
					'message' => 'Profil modifi&eacute; avec succ&egrave;s.',
					'mes_aff' => 'O');

				$this->session->set_userdata($user_a_jour);
			}
			redirect('/administration/monCompte');		
			
		} else {
			$data['connecte'] = $this->session->userdata['prenom'].' '.$this->session->userdata['nom'];
			$data['iduser'] = $this->session->userdata['id'];
			$data['admin'] = $this->session->userdata['admin'];
			$data['uri'] = $this->uri->segment(1);
			
			$this->load->library('layout');
			$this->layout->set_titre('RAVIOLES');
			$this->layout->ajouter_css('ravioles');
			$this->layout->ajouter_css('jquery-ui');
			$this->layout->ajouter_js('jquery');
			$this->layout->ajouter_js('jquery.corner');
			$this->layout->ajouter_js('jquery-ui');
			$this->layout->views('templates/header', $data)
		   				 ->views('administration/monCompte', $data)
		   				 ->view('templates/footer', $data);
		}		
	}
	
	function email_doublon($email) { // Unicité de l'email dans la base de données
		$retour = TRUE;
		if($email != $this->session->userdata['email']) { // Ne faire cette vérification que si la valeur a changé
			$this->load->model('login_model','DBLogin',TRUE);
			$nb_email = $this->DBLogin->count('email', $email);
			if($nb_email == 1) {
				$this->form_validation->set_message('email_doublon', 'Cet e-mail est d&eacute;j&agrave; utilis&eacute;.');
				$retour = FALSE;
			}
		}
		return $retour;
	}
	
	function login_doublon($pseudo) { // Unicit� du login dans la base de données
		$retour = TRUE;
		if($pseudo != $this->session->userdata['login']) { // Ne faire cette vérification que si la valeur a changé
			$this->load->model('login_model','DBLogin',TRUE);	
			$nb_login = $this->DBLogin->count('login', $pseudo);
			if($nb_login == 1) {
				$this->form_validation->set_message('login_doublon', 'Ce nom de login est d&eacute;j&agrave; utilis&eacute;.');
				$retour = FALSE;
			}
		}
		return $retour;
	}
	
	function statut() {
		$this->index('statut');
	}
	
	public function statutsListe() {
		buildGridData(array(
				'model' => 'statuts_model',
				'method' => 'statutsJqgrid',
				'pkid' => 'id',
				'columns' => array( 'id','libelle','suivantId')
				)
		);
	}
	
	public function statutAjout() {
		
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$statut = array();
		$statut['libelle']     = $this->input->post('libelle3');
		$statut['suivantId'] = $this->input->post('suivant');
		
		$statut2 = array(); //champs non échappés : il n'y en a pas.
		
		
		$this->load->model('statuts_model','DBStatut', TRUE);
		$resultat = $this->DBStatut->create($statut, $statut2);
		if($resultat) {
			$statut_a_jour = array(
				'message' => 'Statut \''.$statut['libelle'].'\' ajout&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($statut_a_jour);					
		}
		
		redirect("/administration/statut");		
	}
	
	public function statutMiseajour() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		log_message("info", "### MAJ du statut ".$this->input->post('idstatut')." ###");
		
		$statut = array();
		$statut['libelle']   = $this->input->post('libelle4');
		$statut['suivantId'] = $this->input->post('suivant2');
		
		$statut2 = array(); //champs non échappés : il n'y en a pas.
		
		$where = array('id' => $this->input->post('idstatut'));
		
		$this->load->model('statuts_model','DBStatut', TRUE);
		$resultat = $this->DBStatut->update($where, $statut, $statut2);
		if($resultat) {
			$statut_a_jour = array(
				'message' => 'Statut '.$this->input->post('idstatut').' : '.$this->input->post('libelle').'<br>modifi&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($statut_a_jour);		
		}
		
		redirect('/administration/statut');		
	}
	
	public function statutSuppression() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		$nstatut = $this->input->post('idsstatut');	
		$this->session->set_userdata(array('NStatut' => $nstatut));
		log_message('info','### Suppression du statut '.$nstatut.' ###');
		$this->load->model('statuts_model','DBStatut', TRUE);
		$where = array('id' => $nstatut);
		$resultat = $this->DBStatut->delete($where);
		if($resultat) {
			$statut_a_jour = array(
				'message' => 'Statut '.$nstatut.'<br>supprim&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($statut_a_jour);		
		}
		redirect('/administration/statut');
	}
	
	function parametre() {
		$this->index('parametre');
	}
	
	public function paramsListe() {
		log_message('debug', '### Passage dans paramsListe. ###');
		buildGridData(array(
				'model' => 'params_model',
				'method' => 'paramsJqgrid',
				'pkid' => 'id',
				'columns' => array( 'parametre', 'valeur' )
				)
		);
	}

	public function paramAjout() {
		
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$param = array(
			'parametre' => $this->input->post('paramnom'),
			'valeur'    => $this->input->post('valeur')
		);		
		
		$this->load->model('params_model','DBParam', TRUE);
		$resultat = $this->DBParam->create($param);
		if($resultat) {
			$param_a_jour = array(
				'message' => 'Param&egrave;tre \''.$param['parametre'].'\' ajout&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($param_a_jour);
		}
		
		redirect("/administration/parametre");		
	}
	
	public function paramMiseajour() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		log_message("info", "### MAJ du parametre ".$this->input->post('idparam')." ###");
		
		$param = array(
			'parametre' => $this->input->post('paramnom2'),
			'valeur'    => $this->input->post('valeur2')
		);
		
		$where = array('id' => $this->input->post('idparam'));
		
		$this->load->model('params_model','DBParam', TRUE);
		$resultat = $this->DBParam->update($where, $param);
		if($resultat) {
			$param_a_jour = array(
				'message' => 'Param&egrave;tre '.$this->input->post('idparam').'<br>modifi&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($param_a_jour);		
		}
		
		redirect('/administration/parametre');		
	}
	
	public function paramSuppression() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		$nparam = $this->input->post('idsparam');	
		$this->session->set_userdata(array('NParam' => $nparam));
		log_message('info','### Suppression du param&egrave;tre '.$nparam.' ###');
		$this->load->model('params_model','DBParam', TRUE);
		$where = array('id' => $nparam);
		$resultat = $this->DBParam->delete($where);
		if($resultat) {
			$param_a_jour = array(
				'message' => 'Param&egrave;tre '.$nparam.'<br>supprim&eacute; avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($param_a_jour);		
		}
		redirect('/administration/parametre');
	}

	function categorie() {
		$this->index('categorie');
	}
	
	public function categoriesListe() {
		buildGridData(array(
				'model' => 'categories_model',
				'method' => 'categoriesJqgrid',
				'pkid' => 'id',
				'columns' => array( 'libelle' )
				)
		);
	}

	public function categorieAjout() {
		
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$categorie = array(
			'libelle' => $this->input->post('categlibelle')
		);		
		
		$this->load->model('categories_model','DBCategorie', TRUE);
		$resultat = $this->DBCategorie->create($categorie);
		if($resultat) {
			$categorie_a_jour = array(
				'message' => 'Cat&eacute;gorie \''.$categorie['libelle'].'\' ajout&eacute;e avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($categorie_a_jour);
		}
		
		redirect("/administration/categorie");		
	}
	
	public function categorieMiseajour() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		log_message("info", "### MAJ de la cat&eacute;gorie ".$this->input->post('idcategorie')." ###");
		
		$categorie = array(
			'libelle' => $this->input->post('categlibelle')
		);
		
		$where = array('id' => $this->input->post('idcategorie'));
		
		$this->load->model('categories_model','DBCategorie', TRUE);
		$resultat = $this->DBCategorie->update($where, $categorie);
		if($resultat) {
			$categorie_a_jour = array(
				'message' => 'Cat&eacute;gorie '.$this->input->post('idcategorie').'<br>modifi&eacute;e avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($categorie_a_jour);		
		}
		
		redirect('/administration/categorie');		
	}
	
	public function categorieSuppression() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		$ncategorie = $this->input->post('idscategorie');	
		$this->session->set_userdata(array('NCategorie' => $ncategorie));
		log_message('info','### Suppression de la cat&eacute;gorie '.$ncategorie.' ###');
		$this->load->model('categories_model','DBCategorie', TRUE);
		$where = array('id' => $ncategorie);
		$resultat = $this->DBCategorie->delete($where);
		if($resultat) {
			$categorie_a_jour = array(
				'message' => 'Cat&eacute;gori '.$ncategorie.'<br>supprim&eacute;e avec succ&egrave;s.',
				'mes_aff' => 'O');
			$this->session->set_userdata($categorie_a_jour);		
		}
		redirect('/administration/categorie');
	}
	
	public function mailPerso() {
		if(!isset($this->session->userdata['id'])) {
			show_error("Page interdite sans identification !!");
		}
		
		$uData = array(
			'set_columns' => array(
				array(
					'label' => 'Nom',
					'name' => 'nomprenom',
					'width' => 220,
					'size' => 10
					),
				array(
					'label' => 'À',
					'name' => 'Env',
					'edittype' => 'checkbox',
					'editable' => true,
					'width' => 30,
					'formatter' => 'checkbox',
					'formatoptions' => '{ disabled: false}',
					'editoptions' => '{ value: "True:False",  defaultValue: "False" }}',
					'size' => 10
					),
				),
			'div_name' => 'users_grid',
			'source' => 'administration/usersNomPrenom/',
			'caption' => 'Membres de Ravioles',
			'primary_key' => 'id',
			'loadonce' => 'true',
			'loadComplete' => 'ajoutCheckBox',
			'pager' => 'upager',
			'grid_height' => 250
		);

		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('ObjetMail', 'Objet du mail', 'required');
		$this->form_validation->set_rules('ContenuMail', 'Contenu du mail', 'required');
		
		if($this->form_validation->run()) {
			$mBtn = $this->input->post('BMailPerso');
			if($mBtn == "Envoyer") {	
				$objetMail   = $this->input->post('ObjetMail');
				$contenuMail = $this->input->post('ContenuMail');
				$this->load->helper('envoimail');
				$this->load->model('login_model', 'DBUsers', TRUE);
				$usermail = "";
				$formail = $this->input->post();
				foreach ($formail as $champ => $valeur) {
					if(substr($champ,0,3) == "usr") {
						// On va chercher l'adresse mail du user
						$userid = substr($champ,3);
						$where = array('id' => $userid);
						$du = $this->DBUsers->read('email', $where);
						$dua = $du->result();
						if(!empty($userMail)) { $userMail = $userMail.','; }
						$userMail = $userMail.$dua[0]->email;
					}
				}
				$data_email = array('email' => $userMail, 'objetmail' => $objetMail, 'contenumail' => $contenuMail);
				log_message('debug', '### Envoi email personnalisé à '.$userMail);
				EnvoiMail('PERSONNALISE', $data_email);
				redirect('commandes');
			}
			if($mBtn == "Retour") {	redirect('commandes'); }
		} else {
			$data['usersGrille'] = buildGrid($uData);

			$this->load->library('layout');
			$this->layout->set_titre('RAVIOLES');
			$this->layout->ajouter_css('ravioles');
			$this->layout->ajouter_css('jquery-ui');
			$this->layout->ajouter_css('ui.jqgrid');
			$this->layout->ajouter_js('jquery');
			$this->layout->ajouter_js('jquery.corner');
			$this->layout->ajouter_js('jquery-ui');
			$this->layout->ajouter_js('grid.locale-fr');
			$this->layout->ajouter_js('jquery.jqgrid');
			$this->layout->views('templates/header', $data)
	 				 	 ->views('administration/mailperso',$data)
					 	 ->view('templates/footer', $data);
		}
	}
	
	public function usersNomPrenom() {
		buildGridData(array(
				'model' => 'login_model',
				'method' => 'usersNomPrenomJqgrid',
				'pkid' => 'id',
				'columns' => array( 'nomprenom' )
			)
		);
	}
	
	public function infophp() {
		phpinfo();
	}
	
}